package com.singzi.leetcode.N1200;


import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;



class TreeNode {

    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
     this.left = left;
             this.right = right;
    }
}

public class T1102 {
    public static void main(String[] args) {
       TreeNode t2 = new TreeNode(34);
        TreeNode t3 = new TreeNode(56);
        TreeNode t1 = new TreeNode(12,t2,t3);
        TreeNode t4= new TreeNode(5,t1,null);
        Solution102 s2 = new Solution102();
        System.out.println(s2.levelOrder(t4));
    }
}
class Solution102{
    public List<List<Integer>> levelOrder(TreeNode root) {
        if(root ==null){
            return new ArrayList<>();
        }
        List<List<Integer>> res = new ArrayList();//创建二维数组
        Queue<TreeNode> queque = new LinkedList<TreeNode>();//创建队列
        queque.add(root);//进入队列
        while(!queque.isEmpty()){
            int count = queque.size();
            List<Integer> list = new ArrayList<Integer>();//创建一维数组
            while(count>0){
                TreeNode node = queque.poll();//出队
                list.add(node.val);//
                if(node.left!=null){
                    queque.add(node.left);
                }
                if(node.right!=null){
                    queque.add(node.right);
                }
                count--;
            }
            res.add(list);
        }
        return res;


    }

}